package mtt.us.client.views.menu;

import java.util.HashMap;
import java.util.Map;

import mtt.us.client.events.ShowFachbereichViewEvent;
import mtt.us.client.events.ShowIndexViewEvent;
import mtt.us.client.events.ShowLogoutViewEvent;
import mtt.us.client.views.fachbereich.FachbereichController;
import mtt.us.client.views.fachbereich.FachbereichModel;
import mtt.us.client.views.fachbereich.FachbereichView;
import mtt.us.client.views.fachbereich.events.ShowStudiengangViewEvent;
import mtt.us.client.views.index.IndexController;
import mtt.us.client.views.index.IndexModel;
import mtt.us.client.views.index.IndexView;
import mtt.us.client.views.logout.LogoutController;
import mtt.us.client.views.logout.LogoutModel;
import mtt.us.client.views.logout.LogoutView;
import mtt.us.hmvcFramework.client.hmvc.events.AddViewEvent;

import hmvc4gwt.client.controller.Controller;
import hmvc4gwt.client.event.HmvcEvent;
import hmvc4gwt.client.event.HmvcEventListener;
import hmvc4gwt.client.triad.Triad;

public class MenuController extends Controller implements HmvcEventListener {
	
	/**
	 * enthält alle Views die schon geöffnet wurden
	 */
	private Map mapViews = new HashMap();
	
	public void doInit() {
		/*
		 * MenuEventListener hinzufügen
		 */
		register(MenuEvent.class, this);
		
		fireEvent(new AddViewEvent(getView()), true, false, false);
	}

	public void handleEvent(HmvcEvent event) {
		
		/*
		 * MenuEvent's behandeln
		 */
		if (event instanceof MenuEvent) {
			handleMenuEvent((MenuEvent) event);
		}
	}
	
	/**
	 * behandelt alle MenuEvents
	 */
	private void handleMenuEvent(MenuEvent event) {
		
		Controller controller = null;
		
		if (event.getGotoView() == MenuEvent.viewIndex) {
			
			/*
			 * Fachbereich-View anzeigen
			 */
			Triad triad;
			
			if (mapViews.containsKey(MenuEvent.viewIndex)) {
				/*
				 * Triade aus der Map laden
				 */
				triad = (Triad) mapViews.get(MenuEvent.viewIndex);
			} else {
				/*
				 * neue Triade erstellen und in der Map sichern
				 */
				triad = addChildTriad(new IndexModel(), new IndexView(), new IndexController());
				mapViews.put(MenuEvent.viewIndex, triad);
			}
			
			controller = triad.getController();
			controller.fireLocalEvent(new ShowIndexViewEvent());
			
		} else if (event.getGotoView() == MenuEvent.viewFachbereiche) {
			
			/*
			 * Fachbereich-View anzeigen
			 */
			Triad triad;
			
			if (mapViews.containsKey(MenuEvent.viewFachbereiche)) {
				/*
				 * Triade aus der Map laden
				 */
				triad = (Triad) mapViews.get(MenuEvent.viewFachbereiche);
			} else {
				/*
				 * neue Triade erstellen und in der Map sichern
				 */
				triad = addChildTriad(new FachbereichModel(), new FachbereichView(), new FachbereichController());
				mapViews.put(MenuEvent.viewFachbereiche, triad);
			}

			controller = triad.getController();
			controller.fireLocalEvent(new ShowFachbereichViewEvent());
			
		} else if (event.getGotoView() == MenuEvent.viewStundenpläne) {
			/*
			 * Stundenplan-View anzeigen
			 */
			Triad triad;
			
			if (mapViews.containsKey(MenuEvent.viewStundenpläne)) {
				/*
				 * Triade aus der Map laden
				 */
				triad = (Triad) mapViews.get(MenuEvent.viewStundenpläne);
			} else {
				/*
				 * neue Triade erstellen und in der Map sichern
				 */
//				triad = addChildTriad(new StundenplanModel(), new StundenplanView(), new StundenplanController());
//				mapViews.put(MenuEvent.viewStundenpläne, triad);
				triad = null;
			}
			
			controller = triad.getController();
			controller.fireLocalEvent(new ShowStudiengangViewEvent());
			
		} else if (event.getGotoView() == MenuEvent.viewLogout) {
			
			/*
			 * Logout-View anzeigen
			 */
			Triad triad;
			
			if (mapViews.containsKey(MenuEvent.viewLogout)) {
				/*
				 * Triade aus der Map laden
				 */
				triad = (Triad) mapViews.get(MenuEvent.viewLogout);
			} else {
				/*
				 * neue Triade erstellen und in der Map sichern
				 */
				triad = addChildTriad(new LogoutModel(), new LogoutView(), new LogoutController());
				mapViews.put(MenuEvent.viewLogout, triad);
			}
			
			controller = triad.getController();
			controller.fireLocalEvent(new ShowLogoutViewEvent());
		}
	}
}
